const values_default = {
  state: 'draft',
  company_id: 2001,
  product_id: 3 // TBD 允许 调函数
}

const domain_default = {}

const fields_default = {
  state: null,
  name: null,
  date: null,
  company_id: null,
  partner_id: null,
  product_id: null,
  unit_amount: null,
  quantity: null,
  total_amount: null,
  type: null
}

const filterItems = [
  { type: 'date', name: 'date', placeholder: '账期' },
  {
    type: 'select',
    name: 'partner_id',
    placeholder: '客户',
    options: {
      domain: {
        company_id: 2001,
        company_registry: 'ems',
        comment___like: 'customer'
      }
    }
  }
]

const columns = [
  { name: 'state__name', type: 'char', label: '状态' },
  { name: 'date', type: 'char', label: '日期' },
  { name: 'company_id__name', type: 'char', label: '公司' },
  { name: 'partner_id__name', type: 'char', label: '客户' },
  { name: 'unit_amount', type: 'char', label: '单价' },
  { name: 'quantity', type: 'char', label: '数量' },
  { name: 'total_amount', type: 'char', label: '金额' },
  { name: 'name', type: 'char', label: '摘要' }
]

const formItems = [
  { type: 'input', name: 'name', label: '摘要', rules: { required: false }},
  { type: 'date', name: 'date', label: '日期', default: new Date() },
  {
    type: 'select',
    name: 'partner_id',
    options: {
      domain: {
        company_id: 2001,
        company_registry: 'ems',
        comment___like: 'customer'
      }
    },
    label: '客户',
    placeholder: '客户'
  },
  {
    type: 'number',
    name: 'quantity',
    label: '数量',
    step: 1,
    min: 1,
    default: 1,
    placeholder: '数量'
  }
]

const workflow = {
  states: {
    draft: '起草',
    confirm: '确认',
    deliver: '已发送',
    recieve: '已接收',
    post: '记账',
    done: '支付',
    cancel: '已取消'
  },

  actions: {
    update: { value: '', name: '修改', type: 'primary' },
    draft: {
      name: '重置为草稿',
      method: 'action_draft',
      last_in: ['confirm', 'cancel'],
      type: 'primary'
    },
    confirm: {
      name: '确认',
      method: 'action_confirm',
      last_in: ['draft'],
      type: 'primary'
    },
    deliver: {
      name: '发送',
      method: 'action_deliver',
      last_in: ['confirm'],
      type: 'primary'
    },
    recieve: {
      name: '接收',
      method: 'action_recieve',
      last_in: ['deliver'],
      type: 'primary'
    },
    post: {
      name: '记账',
      method: 'action_post',
      last_in: ['recieve'],
      type: 'primary'
    },
    done: {
      name: '支付',
      method: 'action_done',
      last_in: ['post'],
      type: 'primary'
    },
    cancel: {
      name: '取消',
      method: 'action_cancel',
      last_in: ['confirm', 'deliver']
    }
  },

  transfers: {
    draft: {
      actions: ['update', 'confirm'],
      states: ['draft', 'confirm', 'deliver', 'recieve', 'post', 'done']
    },
    confirm: {
      actions: ['deliver', 'draft', 'cancel'],
      states: ['draft', 'confirm', 'deliver', 'recieve', 'post', 'done']
    },
    deliver: {
      actions: ['recieve', 'cancel'],
      // actions: [],
      states: ['draft', 'confirm', 'deliver', 'recieve', 'post', 'done']
    },
    recieve: {
      actions: ['post'],
      states: ['draft', 'confirm', 'deliver', 'recieve', 'post', 'done']
    },
    post: {
      actions: ['done'],
      states: ['draft', 'confirm', 'deliver', 'recieve', 'post', 'done']
    },
    done: {
      actions: [],
      states: ['draft', 'confirm', 'deliver', 'recieve', 'post', 'done']
    },
    cancel: {
      actions: ['draft'],
      states: {
        confirm: ['draft', 'confirm', 'cancel'],
        deliver: ['draft', 'confirm', 'deliver', 'cancel']
      }
    }
  }
}

const Model = {
  configs: {
    workflow,
    values_default,
    domain_default,
    fields_default,
    filterItems,
    columns,
    formItems
  },

  extend: BaseClass => {
    class ModelClass extends BaseClass {
      global_domain(domain_src = {}) {
        // console.log('this.global_domain in odoo.res.partner')
        const domain = { ...domain_src }
        // const userinfo = self.get_userinfo()
        // const { uid } = userinfo
        // domain.user_id = uid
        return super.global_domain(domain)
      }

      async action_draft(rid) {
        await this.call('action_draft', [rid])
        return true
      }
      async action_cancel(rid, kwargs = {}) {
        await this.call('action_cancel', [rid])
        return true
      }
      async action_confirm(rid) {
        await this.call('action_confirm', [rid])
        return true
      }
      async action_deliver(rid) {
        await this.call('action_deliver', [rid])
        return true
      }
      async action_recieve(rid) {
        await this.call('action_recieve', [rid])
        return true
      }
      async action_post(rid) {
        await this.call('action_post', [rid])
        return true
      }
      async action_done(rid) {
        await this.call('action_done', [rid])
        return true
      }
    }
    return ModelClass
  }
}

export default Model
